//app.js

App({
  setting: function(){
    var that = this;
    wx.getUserInfo({
      success: res => {
        // 可以将 res 发送给后台解码出 unionId
        that.globalData.userInfo = res.userInfo

        // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
        // 所以此处加入 callback 以防止这种情况
        if (that.userInfoReadyCallback) {
          that.userInfoReadyCallback(res)
        }
      }
    })
  },
  onLaunch: function () {
    var that = this;
    var openid = that.globalData.udata.openid;
    //wx.showLoading({ title: '登录中… '})
    // 获取用户信息
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          that.globalData.setting = true;
          that.setting()
          /*wx.authorize({
            scope: 'scope.userInfo',
            success() { 
              that.setting()
            },
            fail() { }
          })*/
        }
        that.globalData.settingf = true;
        if (that.settingCallback) {
          that.settingCallback(res)
        }
      }
    })
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        if (res.code) {
          console.log('code为：'+res.code)
          //发起网络请求,获取openid和session_key
          wx.request({
            url: that.globalData.url +'/getopenid',
            method: 'POST',
            header: {
              'content-type': 'application/json'
            },
            data: {
              code: res.code
            },
            success: function (res) {
              if (res.statusCode == 200) {
                openid = res.data.openid
                console.log("获取到的openid为：" + openid)
                // wx.setStorageSync('openid', res.data) 
                if (that.globalData.userInfo){
                  that.login(openid);
                }else{
                  that.userInfoReadyCallback = res => {
                    that.login(openid);
                  }
                }
              } else {
                console.log('res.statusCode != 200   ' + res.errMsg)
              }
            }
          })
        } else {
          console.log('获取用户登录码失败！' + res.errMsg)
        }
      }
    })
    
  },
  globalData: {
    url: 'https://www.zfac.club',
    setting:false,
    udata:{
      openid:-1,
      uname:'regist',
      buildTime:'',
      lastTime:'',
      countCard:-1
    }
  },
  login:function(openid){
    var that = this;
    wx.request({
      url: that.globalData.url + '/login',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        openid: openid,
        uname: that.globalData.userInfo.nickName
      },
      success: function (res) {
        if (res.statusCode == 200) {
          //console.log(res.data)
          that.globalData.udata = res.data;
          console.log(that.globalData.udata)
          var time = that.formatUTC(res.data.buildTime)
          console.log(time)
          if (that.udataCallback) {
            that.udataCallback(res)
          }
          if (that.udataCallback2) {
            that.udataCallback2(res)
          }

          //wx.hideLoading()

          // wx.setStorageSync('openid', res.data) 
        } else {
          console.log('res.statusCode != 200   ' + res.errMsg)
        }
      }
    })
  },
  formatUTC: function(utc_datetime) { 
    var date = new Date(utc_datetime);
    var year = date.getFullYear();
    var month = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1);
    var day = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate());
    var hour = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours() ) ;
    var minute =  (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes());
    var second =  (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds());
    return year + '/' + month + '/' + day + ' ' + hour + ':' + minute + ':' + second;
  }
})